<?php

require_once 'util/class.daofactory.php';
require_once 'util/class.encrypt.php';
require_once 'util/class.idservice.php';

class UserService {
	
	public function register($nick, $email, $password, $ip, $src) {
		$result = array();
		if(!$nick || strlen($nick)==0 
				|| !$email || strlen($email)==0 
				|| !$password || strlen($password)==0) {
			$result['status']=Constants::$ERROR_INVALID_ARGS;
			return $result;
		}
		global $userDao;
		global $sessionDao;
		// step1, check email
		$user = $userDao->getUserByEmail($email);
		if($user) {
			$result['status']=Constants::$ERROR_REGISTER_EMAIL_EXISTS;
			return $result;
		}
		
		// step2, check nick
		$user = $userDao->getUserByNick($nick);
		if($user) {
			$result['status']=Constants::$ERROR_REGISTER_NICK_EXISTS;
			return $result;
		}
		// step3, create account
		$userId = IdService::getNextId("user");
		if(!$userId || $userId<0) {
			$result['status']=Constants::$ID_ERROR;
			return $result;
		}
		$ts = time();
		$encryptPwd = EncryptUtil::encrypt($password, $nick.Constants::$ENCRYPT_KEY);
		$userDao->addUser($userId, $nick, $encryptPwd, $email, $src, $ip, $ts);
		
		// step4, add to session
		$skey = md5($encryptPwd.$ts);
		$sessionDao->addSessionItem($userId, $skey, $ts);
		
		// step5, return id and token
		$result['status']=Constants::$SUCCESS;
		$result['data']=array('id'=>$userId, 'skey'=>$skey);
		return $result;
	}
	
	public function modifyProfile($userId, $nick, $password, $birthday, $profile) {
		$result = array();
		if(!$userId || !is_int($userId) || $userId<=0) {
			$result['status']=Constants::$ERROR_INVALID_ARGS;
			return $result;
		}
		global $userDao;
		global $sessionDao;
		$values=array();
		if(!$nick || strlen($nick)!=0)
			$values['nick']=$nick;
		
		if(!$password || strlen($password)!=0)
			$values['pwd']=EncryptUtil::encrypt($password, $nick.Constants::$ENCRYPT_KEY);
		
		if(!$birthday || strlen($birthday)!=0)
			$values['birthday']=$birthday;
		
		if(!$profile || strlen($profile)!=0)
			$values['profile']=$profile;
		
		$userDao->updateFields($userId, $values);
		$result['status']=Constants::$SUCCESS;
		$result['data']=array();
		return $result;
		
	}
	
	
	public function login($email, $password) {
		$result = array();
		if(!$email || strlen($email)==0 || !$password || strlen($password)==0) {
			$result['status']=Constants::$ERROR_INVALID_ARGS;
			return $result;
		}
		global $userDao;
		global $sessionDao;
		
		// step1, check the password
		$user = $userDao->getUserByEmail($email);
		if($user==null) {
			$result['status']=Constants::$ERROR_LOGIN_USERNOTEXISTS;
			return $result;
		}
		
		$encryptedPwd = EncryptUtil::encrypt($password, $user['nick'].Constants::$ENCRYPT_KEY);
		if($user['pwd']!= $encryptedPwd) {
			$result['status']=Constants::$ERROR_LOGIN_PASSWORDERROR;
			return $result;
		}
		
		// step2, update last login time
		$ts = time();
		$userDao->updateField($user['_id'], 'lastLogin', $ts);
		// step3, generate the skey and update refresh time
		$skey = md5($encryptedPwd.$ts);
		$sessionDao->updateSession($user['_id'], $ts, $skey);
		
		// step4, return skey
		$result['status']=Constants::$SUCCESS;
		$result['data']=array('skey'=>$skey);
		return $result;
	}
	
	public function logout($userId, $skey) {	
		$result = array();
		if( !$userId || !is_int($userId) || $userId<=0
				|| !$skey || strlen($skey)==0) {
			$result['status']=Constants::$ERROR_INVALID_ARGS;
			return $result;
		}
		global $userDao;
		global $sessionDao;
		// step1, check the skey
		$sessionItem = $sessionDao->getSessionItem($userId);
		if(!$sessionItem) {
			$result['status']=Constants::$ERROR_LOGOUT_NOTLOGIN;
			return $result;
		}
		
		if($sessionItem['skey']!=$skey) {
			$result['status']=Constants::$ERROR_LOGOUT_ERRORSKEY;
			return $result;
		}
		
		// step2, remove the user from session
		$sessionDao->removeSessionItem($userId);
		$result['status']=Constants::$SUCCESS;
		$result['data']=array();
		return $result;
	}
	
	public function getUserBasicInfoByName($name) {
		if(!isset($name) || strlen($name)==0) {
			$result['status']=Constants::$ERROR_INVALID_ARGS;
			return $result;
		}
		
		global $userDao;
		if($userDao==null) {
			$result['status']=Constants::$ERROR_DB;
			return $result;
		}
		
		$info = $userDao->getUserBasicInfoByname($name);
		if($info==null) {
			$result['status']=Constants::$ERROR_NOT_EXIT;
		} else {
			$result['status']=Constants::$SUCCESS;
			$result['data'] = $info;
		}
		return $result;
	}
	
	public function searchUserByName($name, $offset, $limit) {
		if(!isset($name) || strlen($name)==0 || !is_numeric($offset) || !is_numeric($limit)) {
			$result['status']=Constants::$ERROR_INVALID_ARGS;
			return $result;
		}
	
		global $userDao;
		if($userDao==null) {
			$result['status']=Constants::$ERROR_DB;
			return $result;
		}
		$info = $userDao->searchUserByNick($name, $offset, $limit);
		if($info==null) {
			$result['status']=Constants::$ERROR_NOT_EXIT;
		} else {
			$result['status']=Constants::$SUCCESS;
			$result['data'] = $info;
		}
		return $result;
	}
	
	public function recommendUser($num) {
		if(!is_numeric($num) || $num<0) {
			$result['status']=Constants::$ERROR_INVALID_ARGS;
			return $result;
		}
	
		global $userDao;
		if($userDao==null) {
			$result['status']=Constants::$ERROR_DB;
			return $result;
		}
		$info = $userDao->randomRecommendUser($num);
		if($info==null) {
			$result['status']=Constants::$ERROR_NOT_EXIT;
		} else {
			$result['status']=Constants::$SUCCESS;
			$result['data'] = $info;
		}
		return $result;
	}
	
	public function getUserBasicInfo($userId) {
		$result = array();
		if($userId==null || !is_int($userId) || $userId<=0) {
			$result['status']=Constants::$ERROR_INVALID_ARGS;
			return $result;
		}
		
		global $userDao;
		global $sessionDao;
		if($userDao==null) {
			$result['status']=Constants::$ERROR_DB;
			return $result;
		}
		
		$info = $userDao->getUserBasicInfo($userId);
		if($info==null) {
			$result['status']=Constants::$ERROR_NOT_EXIT;
		} else {
			$result['status']=Constants::$SUCCESS;
			$result['data'] = $info;
		}
		return $result;
	}
	
	public function postAdvice($userId, $text) {
		$result = array();
		if(!is_int($userId) || $userId<=0 || !isset($text)) {
			$result['status']=Constants::$ERROR_INVALID_ARGS;
			return $result;
		}
	
		global $adviceDao;
		if($adviceDao==null) {
			$result['status']=Constants::$ERROR_DB;
			return $result;
		}
		
		$adviceId = IdService::getNextId('advice');
		if($adviceId<0) {
			$result['status']=$adviceId;
			return $result;
		}
		$ts = time();
		$adviceDao->addAdvice($adviceId, $userId, $text, $ts);
		$result['status']=Constants::$SUCCESS;
		return $result;
	}

	
}